package interfaz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Dialog extends Container implements ActionListener
{
	//----------Variables------------------
	private JDialog _dialog;
	private JButton _ok;
	private JTextField _entrada;
	private String _text;
	//------------------------------------
	
	@SuppressWarnings("deprecation")
	public Dialog()
	{
		this.setSize(50,80); //here you can set the size of the container and dialog	
	    Window window = new JFrame();
	    
	    if (window instanceof JFrame) 
	    {
	    	_dialog = new JDialog((JFrame)window, "Entrada", true);	
	    }    
	    _entrada = new JTextField();
		_entrada.addKeyListener( new KeyListener()
		{
			@Override
			public void keyPressed( KeyEvent _e ) 
			{
				// TODO Auto-generated method stub
				if( _e.getKeyCode() == KeyEvent.VK_ENTER )
				{
					_text = _entrada.getText();
					_dialog.dispose(); //This will exit the dialog and the program will go on
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) 
			{
				// TODO Auto-generated method stub
			}
			@Override
			public void keyTyped(KeyEvent arg0) 
			{
				// TODO Auto-generated method stub	
			}
			
		});
		
		_dialog.add(_entrada, BorderLayout.NORTH);
		
	    _dialog.setLocationRelativeTo(null); //this function will set the dialog in the middle of the parent component
	    _dialog.getContentPane().add(this); //here you place the container in the dialog	   
	    _dialog.getContentPane().setBackground(Color.white); //here you set the background color, you can delete thisone if you like gray
	    _dialog.setSize(this.getPreferredSize()); //the dialog will resize to the size of your container, look at MyContainer for the size
	    
	    _ok = new JButton("OK");
	    _ok.addActionListener(this);
	    
	    _dialog.add(_ok, BorderLayout.CENTER);
	    _dialog.show(); //and show the dialog
	}

	public void setDialog(JDialog _dialog)
	{
		this._dialog = _dialog;
	}

	@Override
	public void actionPerformed( ActionEvent _e ) 
	{
		// TODO Auto-generated method stub
		if( _e.getSource() == _ok )
		{
			_text = _entrada.getText();
			_dialog.dispose(); //This will exit the dialog and the program will go on
		}
	}
	
	public String dameEntrada()
	{
		return _text;
	}

}